const express = require('express');
const ChatController = require('../../controllers/chatController');
const { validateRequest } = require("../../middleware/validationMiddleware");
const { body, param, query } = require("express-validator");
const multer = require('multer');

const router = express.Router();

// 配置multer用于处理文件上传
const storage = multer.memoryStorage();
const upload = multer({ 
  storage: storage,
  limits: {
    fileSize: 5 * 1024 * 1024, // 限制5MB
    files: 1 // 限制单次上传1个文件
  },
  fileFilter: (req, file, cb) => {
    if (file.mimetype.startsWith('image/')) {
      cb(null, true);
    } else {
      cb(new Error('只允许上传图片文件'), false);
    }
  }
});

// 基础聊天接口
router.post('/chat',
  [
    body("humanMessage").notEmpty().withMessage("humanMessage参数不能为空"),
    body("systemMessage").optional()
    .isString()
    .withMessage("systemMessage参数必须为字符串"),
  ],
  validateRequest, 
  ChatController.chat
);

// 基于检索增强生成（RAG）的聊天接口
router.post('/ragChat', ChatController.ragChat);

// 多模态聊天接口
router.post('/visionChat',
  upload.fields([
    // 指定文件字段
    { name: 'image', maxCount: 1 }, // 'avatar'字段，最多允许1个文件
    { name: 'question', maxCount: 8 } // 'gallery'字段，最多允许8个文件
  ]),
  [
    body("question").notEmpty().withMessage("问题内容不能为空"),
  ],
  validateRequest,
  ChatController.visionChat
);

module.exports = router;